using System.Text;
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;
using BA.DAL;
using BA;

namespace BlazeApps.Library.Page
{
    [Serializable()]
    public class PageLock: Csla.BusinessBase<PageLock>
    {
        private BlazeApps.Library.ConfigSettings Config = new BlazeApps.Library.ConfigSettings();
        private BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
        private Procedures Procs = new Procedures();

        #region Business Properties and Methods

        //declare members
        private Guid _pageid = Guid.NewGuid();
        private Guid _currentuserid = Guid.NewGuid();

        [System.ComponentModel.DataObjectField(true, true)]
        public Guid pageid
        {
            get
            {
                CanReadProperty("pageid", true);
                return _pageid;
            }
            set
            {
                CanWriteProperty("pageid", true);
                if (!_pageid.Equals(value))
                {
                    _pageid = value;
                    PropertyHasChanged("pageid");
                }
            }
        }

        public Guid currentuserid
        {
            get
            {
                CanReadProperty("currentuserid", true);
                return _currentuserid;
            }
            set
            {
                CanWriteProperty("currentuserid", true);
                if (!_currentuserid.Equals(value))
                {
                    _currentuserid = value;
                    PropertyHasChanged("currentuserid");
                }
            }
        }

        protected override object GetIdValue()
        {
            return _pageid;
        }

        #endregion //Business Properties and Methods

        #region Validation Rules
        private void AddCustomRules()
        {
            //add custom/non-generated rules here...
        }

        private void AddCommonRules()
        {
        }

        protected override void AddBusinessRules()
        {
            AddCommonRules();
            AddCustomRules();
        }
        #endregion //Validation Rules

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
        }


        public static bool CanGetObject()
        {
            //TODO: Define CanGetObject permission in PageLock
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("PageLockViewGroup"))
            //	return true;
            //return false;
        }

        public static bool CanAddObject()
        {
            //TODO: Define CanAddObject permission in PageLock
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("PageLockAddGroup"))
            //	return true;
            //return false;
        }

        public static bool CanEditObject()
        {
            //TODO: Define CanEditObject permission in PageLock
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("PageLockEditGroup"))
            //	return true;
            //return false;
        }

        public static bool CanDeleteObject()
        {
            //TODO: Define CanDeleteObject permission in PageLock
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("PageLockDeleteGroup"))
            //	return true;
            //return false;
        }
        #endregion //Authorization Rules

        #region Factory Methods
        private PageLock()
        { /* require use of factory method */
        }

        public static PageLock NewPageLock()
        {
            if (!CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a PageLock");
            return DataPortal.Create<PageLock>();
        }

        public static PageLock GetPageLock(Guid pageid)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a PageLock");
            return DataPortal.Fetch<PageLock>(new Criteria(pageid));
        }

        public static void DeletePageLock(Guid pageid, Guid currentuserid)
        {
            if (!CanDeleteObject())
                throw new System.Security.SecurityException("User not authorized to remove a PageLock");
            DataPortal.Delete(new Criteria(pageid, currentuserid));
        }

        public override PageLock Save()
        {
            if (IsDeleted && !CanDeleteObject())
                throw new System.Security.SecurityException("User not authorized to remove a PageLock");
            else if (IsNew && !CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a PageLock");
            else if (!CanEditObject())
                throw new System.Security.SecurityException("User not authorized to update a PageLock");

            return base.Save();
        }

        #endregion //Factory Methods

        #region Data Access

        #region Criteria

        [Serializable()]
        private class Criteria
        {
            public Guid pageid;
            public Guid currentuserid;

            public Criteria(Guid pageid)
            {
                this.pageid = pageid;
            }

            public Criteria(Guid pageid, Guid currentuserid)
            {
                this.pageid = pageid;
                this.currentuserid = currentuserid;
            }
        }

        #endregion //Criteria

        #region Data Access - Create
        [RunLocal]
        protected override void DataPortal_Create()
        {
            ValidationRules.CheckRules();
        }

        #endregion //Data Access - Create

        #region Data Access - Fetch
        private void DataPortal_Fetch(Criteria criteria)
        {
            ExecuteFetch(criteria);
        }

        private void ExecuteFetch(Criteria criteria)
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            DAL.AddParameter("@pageid", criteria.pageid);

            using(SafeDataReader dr = new SafeDataReader(DAL.ExecuteReader(Procs.GetPageLockByPageID.ToString().Trim(), CommandType.Text, ConnectionState.Open)))
            {
                FetchObject(dr);
                ValidationRules.CheckRules();

                //load child object(s)
                FetchChildren(dr);
            }
        }

        private void FetchObject(SafeDataReader dr)
        {
            dr.Read();
            _pageid = dr.GetGuid("pageid");
            _currentuserid = dr.GetGuid("currentuserid");
        }

        private void FetchChildren(SafeDataReader dr)
        {
        }
        #endregion //Data Access - Fetch

        #region Data Access - Insert
        protected override void DataPortal_Insert()
        {
            ExecuteInsert();
        }

        private void ExecuteInsert()
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddInsertParameters(ref DAL);

            DAL.ExecuteNonQuery(Procs.InsertPageLock.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddInsertParameters(ref BA.DAL.DBUtility DAL)
        {
            DAL.AddParameter("@pageid", _pageid);
            DAL.AddParameter("@userid", _currentuserid);
        }
        #endregion //Data Access - Insert

        #region Data Access - Update
        protected override void DataPortal_Update()
        {
            if (base.IsDirty)
            {
                ExecuteUpdate();
            }
        }

        private void ExecuteUpdate()
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddUpdateParameters(ref DAL);

            DAL.ExecuteNonQuery(Procs.UpdatePageLock.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddUpdateParameters(ref BA.DAL.DBUtility DAL)
        {
            DAL.AddParameter("@pageid", _pageid);
            DAL.AddParameter("@userid", _currentuserid);
        }

        private void UpdateChildren(SqlConnection cn)
        {
        }
        #endregion //Data Access - Update

        #region Data Access - Delete
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new Criteria(_pageid));
        }

        private void DataPortal_Delete(Criteria criteria)
        {
            ExecuteDelete(criteria);
        }

        private void ExecuteDelete(Criteria criteria)
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddDeleteParameters(ref DAL, criteria);

            DAL.ExecuteNonQuery(Procs.DeletePageLock.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddDeleteParameters(ref BA.DAL.DBUtility DAL, Criteria criteria)
        {
            DAL.AddParameter("@pageid", criteria.pageid);
            DAL.AddParameter("@userid", criteria.currentuserid);
        }
        #endregion //Data Access - Delete
        #endregion //Data Access
    }
}
